package com.briup.app02.web.controller;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.app02.bean.Category;
import com.briup.app02.bean.Product;
import com.briup.app02.bean.extend.ProductExtend;
import com.briup.app02.service.IProductService;
import com.briup.app02.utils.Message;
import com.briup.app02.utils.MessageUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/product")
@Api(description="产品信息相关接口")
public class ProductController {
	
	@Autowired
	private IProductService productService;
	
	@ApiOperation(value="查询产品信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name="name",value = "产品名称",paramType = "query")
	})
	@GetMapping("query")
	public Message query(String name,String status,Long productCategoryId) {
		List<Product>list=productService.query(name, status, productCategoryId);
		return MessageUtil.success(list);
	}
	
	//级联查询
	@ApiOperation(value="级联查询产品信息")
	@GetMapping("queryCascade")
	public Message queryCascade(String name,String status,Long productCategoryId) {
		List<ProductExtend>list=productService.queryCascade(name, status, productCategoryId);
		return MessageUtil.success(list);
	}
	
	//分页查询
	@ApiOperation(value = "分页查询产品信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name="page",value = "当前页",paramType = "query",required = true),
		@ApiImplicitParam(name="pageSize",value = "每页大小",paramType = "query",required = true),
		@ApiImplicitParam(name="name",value = "产品名称",paramType = "query"),
		@ApiImplicitParam(name="status",value = "产品状态",paramType = "query"),
		@ApiImplicitParam(name="productCategoryId",value = "分类编号",paramType = "query"),
	})
	@GetMapping("pageQuery")
	public Message pageQuery(int page,int pageSize,String name,String status,Long productCategoryId) {
		List<ProductExtend>list=productService.pageQuery(page, pageSize, name, status, productCategoryId);
		return MessageUtil.success(list);
	}

	@ApiOperation(value="通过id删除产品")
	@GetMapping("deleteById")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true)
	}) 
	public Message deleteById(@NonNull Long id) {
		productService.deleteById(id);
		return MessageUtil.success("删除成功");
	}
	
	@ApiOperation(value="保存或更新产品信息")
	
	@ApiImplicitParams({
		@ApiImplicitParam(name="id",value = "主键",paramType = "form"),
        @ApiImplicitParam(name="name",value = "产品名称",paramType = "form"),
        @ApiImplicitParam(name="description",value = "产品介绍",paramType = "form"),
        @ApiImplicitParam(name="status",value = "产品状态",paramType = "form"),
        @ApiImplicitParam(name="productCategoryId",value = "分类编号",paramType = "form"),
        @ApiImplicitParam(name="price",value = "单价",paramType = "form"),
        @ApiImplicitParam(name="photo",value = "图片",paramType = "form"),
	})
	@PostMapping("saveOrUpdate")
	public Message saveOrUpdate(Product product) {
		productService.saeOrUpdate(product);
		return MessageUtil.success("更新成功");
	}
	
	@ApiOperation(value="上架")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id",value = "产品编号",paramType = "query",required = true),
	})
	@GetMapping("online")
	public Message online(Long id) {
		productService.online(id);
		return MessageUtil.success("上架成功");
	}
	
	@ApiOperation(value="下架")
	@ApiImplicitParams({
		 @ApiImplicitParam(name="id",value = "产品编号",paramType = "query",required = true),
	})
	@GetMapping("offline")
	public Message offline(Long id) {
		productService.offline(id);
		return MessageUtil.success("下架成功");
	}
}
	
	/*@GetMapping("findAll")
	@ApiOperation("查询所有产品信息")
	public List<Product> findAll(){
		return productService.findAll();
	}
	
	
	@GetMapping("selectById")
	@ApiOperation("根据id查询产品信息")
	@ApiImplicitParam(name ="id",value = "产品id",dataType = "long",paramType = "query",required = true)
	public Product findById(long id) {
		return productService.findById(id); 
	}
	
	@PostMapping("saveOrUpdate")
	@ApiOperation(value="保存或更新",notes = "id为空是保存否则是更新")
	public String saveOrUpdate(Product product) {
		productService.saveOrUpdate(product);
		return "操作成功";
	}
	
	@DeleteMapping("deleteById")
	@ApiOperation("根据id删除产品信息")
	@ApiImplicitParam(name="id",value = "产品id",dataType = "long",paramType = "query",required = true)
	public String deleteById(long id) {
		productService.deleteById(id);
		return("删除成功");
		
		}*/
	
	

